package com.lvchen.common.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lvchen.R;
import com.lvchen.common.base.AppManager;
import com.lvchen.common.myinterface.DiologState;
import com.lvchen.common.view.AppUpdateDialog;
import com.lvchen.common.view.CommonDialog;
import com.lvchen.common.view.LoadingDialog;
import com.mingle.widget.LoadingView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;


/**
 * UI帮助类
 */
public class UIHelper {

    public static void toast(Context context, String content, boolean status){
        View view= LayoutInflater.from(context).inflate(R.layout.layout_toast, null);      //加载布局文件
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(DisyplayUtils.getScreenWidth(context), DisyplayUtils.getScreenHeight(context));
        TextView textView=(TextView) view.findViewById(R.id.toast_text);    // 得到textview
        ImageView img_toast=(ImageView)view.findViewById(R.id.img_toast);
        if (status){
            img_toast.setImageDrawable(context.getResources().getDrawable(R.mipmap.img_ok));
        }else {
            img_toast.setImageDrawable(context.getResources().getDrawable(R.mipmap.img_error));
        }
        RelativeLayout rl_toast=(RelativeLayout)view.findViewById(R.id.rl_toast);
        rl_toast.setLayoutParams(params);
        textView.setText(content);     //设置文本类荣，就是你想给用户看的提示数据
        Toast toast=new Toast(context);     //创建一个toast
        toast.setDuration(Toast.LENGTH_SHORT);          //设置toast显示时间，整数值
        toast.setGravity(Gravity.CENTER, Gravity.CENTER, Gravity.CENTER);    //toast的显示位置，这里居中显示
        toast.setView(view);     //設置其显示的view,
        toast.show();             //显示toast
    }

    public static LoadingDialog loadingDialog;

    public static void ShowLoadingView(String content){
        if (loadingDialog != null) {
            loadingDialog.dismiss();
            loadingDialog.cancel();
            loadingDialog = null;
        }
        Activity activity = AppManager.getAppManager().getActivity();
        if (activity == null||activity.isFinishing()) {
            return;
        }
        loadingDialog = new LoadingDialog(activity, R.style.loading_dialog,R.layout.layout_loadingview);
        loadingDialog.setCanceledOnTouchOutside(false);
        loadingDialog.setCancelable(false);
        loadingDialog.show();
        loadingDialog.setText(content);
        activity = null;
    }

    public static void CloseLoadingView(){
        if (loadingDialog != null){
            loadingDialog.dismiss();
            loadingDialog.cancel();
            loadingDialog=null;
        }
    }


    /**
     * 公共dialog
     * */
    public static void showcommondialog(Context context, String title, String msg, String sure , final String cancal, final DiologState diologState) {
        CommonDialog dialog = new CommonDialog(context).builder().setCancelable(false)
                .setTitle(title, msg)
                .setPositiveButton(sure, new View.OnClickListener() {
                    @Override
                    public void onClick(View view1) {
                        diologState.success();
                    }
                }).setNegativeButton(cancal, new View.OnClickListener() {
                    @Override
                    public void onClick(View view2) {
                        diologState.failure();
                    }
                });
        dialog.show();
    }

    /**
     * 更新dialog
     * */
    public static void showappupdatedialog(Context context, String title, String msg, String sure , final String cancal, final DiologState diologState) {
        AppUpdateDialog dialog = new AppUpdateDialog(context).builder().setCancelable(false)
                .setTitle(title, msg)
                .setPositiveButton(sure, new View.OnClickListener() {
                    @Override
                    public void onClick(View view1) {
                        diologState.success();
                    }
                }).setNegativeButton(cancal, new View.OnClickListener() {
                    @Override
                    public void onClick(View view2) {
                        diologState.failure();
                    }
                });
        dialog.show();
    }

    /**
     * 根据路径获得突破并压缩返回bitmap用于显示
     *
     * @param
     * @return
     */
    public static Bitmap getSmallBitmap(String filePath) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);
        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, 500, 700);
        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(filePath, options);
    }

    /**
     * 计算图片的缩放值
     *
     * @param options
     * @param reqWidth
     * @param reqHeight
     * @return
     */
    public static int calculateInSampleSize(BitmapFactory.Options options,int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 4;

        if (height > reqHeight || width > reqWidth) {

            // Calculate ratios of height and width to requested height and
            final int heightRatio = Math.round((float) height / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);

            // Choose the smallest ratio as inSampleSize value, this will
            // a final image with both dimensions larger than or equal to the
            // requested height and width.
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }

        return inSampleSize;
    }

}
